//
//  HJRCIMManager.m
//  project
//
//  Created by hu on 2019/3/26.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJRCIMManager.h"

@implementation HJRCIMManager

+(instancetype)manager {
    static HJRCIMManager * manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[HJRCIMManager alloc]init];
    });
    return manager;
}

+(void)load {
    [self manager];
}
-(instancetype)init {
    if (self = [super init]) {
        [mNotificationCenter addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
            [self connetRIM];
        }];
    }
    return self;
}
#pragma mark -- 连接服务器
-(void)connetRIM {
    NSString * RC_token = [HJUserManager getUserModel].rongCloudToken;
    if (RC_token) {
        [[RCIMClient sharedRCIMClient] connectWithToken:RC_token success:^(NSString *userId) {
            NSLog(@"userid==%@",userId);
        } error:^(RCConnectErrorCode status) {
            NSLog(@"登录错误%ld",(long)status);
        } tokenIncorrect:^{
            NSLog(@"Token错误或者过期");
            [self getRongCloudToken];
        }];
    }else {
        if (![HJUserManager isLogin]) return;
        // 访问自己服务器获取融云的token值
        [self getRongCloudToken];
    }
}
#pragma mark -- 访问自己服务器获取融云的token值
-(void)getRongCloudToken {
    [HJUserManager syncUserModelWithCompletion:^(HJUserModel *model) {
        [[RCIMClient sharedRCIMClient] connectWithToken:model.rongCloudToken success:^(NSString *userId) {
            NSLog(@"userid==%@",userId);
        } error:^(RCConnectErrorCode status) {
            NSLog(@"登录错误%ld",(long)status);
        } tokenIncorrect:^{
            NSLog(@"Token错误或者过期");
        }];
    }];
}

-(void)configRC {
    RCUserInfo * currentInfo = [[RCUserInfo alloc]initWithUserId:[HJUserManager getUserModel].userId name:[HJUserManager getUserModel].nickName portrait:[HJUserManager getUserModel].avatar];
    [RCIMClient sharedRCIMClient].currentUserInfo = currentInfo;
    // 用户头像样式
    [RCIM sharedRCIM].globalConversationAvatarStyle = RC_USER_AVATAR_CYCLE;
    // 日志
    [RCIMClient sharedRCIMClient].logLevel = RC_Log_Level_Info;
    [RCIM sharedRCIM].enableMessageAttachUserInfo = YES;
    // 开启持久化
    [RCIM sharedRCIM].enablePersistentUserInfoCache = YES;
    
    // 开启消息回撤功能
    //    [RCIM sharedRCIM].enableMessageRecall = YES;
    // 多端未读数同步
    [RCIM sharedRCIM].enableSyncReadStatus = YES;
    // 未注册消息
    [RCIM sharedRCIM].showUnkownMessage = YES;
    [RCIM sharedRCIM].showUnkownMessageNotificaiton = YES;
    // 设置容云 IM
    // 是否在发送的所有消息中携带当前登陆的用户信息
    [RCIM sharedRCIM].enableMessageAttachUserInfo = YES;
    // 开启持久化
    [RCIM sharedRCIM].enablePersistentUserInfoCache = YES;
    // 设置用户信息源
    [RCIM sharedRCIM].userInfoDataSource = self;
    // 开启消息回撤功能
    [RCIM sharedRCIM].enableMessageRecall = YES;
    // 多端未读数同步
    [RCIM sharedRCIM].enableSyncReadStatus = YES;
    // 未注册消息
    [RCIM sharedRCIM].showUnkownMessage = YES;
    [RCIM sharedRCIM].showUnkownMessageNotificaiton = YES;
}

#pragma mark -- RCIMUserInfoDataSource 获取单聊消息
-(void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *))completion {
    RCUserInfo * userInfo = [RCUserInfo new];
    if (userId == nil || [userId length]==0) {
        userInfo.userId = userId;
        userInfo.portraitUri = @"";
        userInfo.name = @"";
        completion(userInfo);
        return;
    }
    // 从自己服务器获取单聊用户信息 传给融云SDK
    if ([userId isEqualToString:[HJUserManager getUserModel].userId]) {
        RCUserInfo * currentInfo = [[RCUserInfo alloc]initWithUserId:[HJUserManager getUserModel].userId name:[HJUserManager getUserModel].nickName portrait:[HJUserManager getUserModel].avatar];
        [RCIMClient sharedRCIMClient].currentUserInfo = currentInfo;
        dispatch_async(dispatch_get_main_queue(), ^{
            return completion(userInfo);
        });
    }else {
        NSString * url = [Api stringByAppendingString:QueryByIdApi];
        [HJHttpTool get:url params:@{@"userId":userId} success:^(HJResponeds * _Nonnull result) {
            HJUserModel *model = [HJUserModel yy_modelWithDictionary:result.data];
            RCUserInfo *userInfo = [[RCUserInfo alloc] initWithUserId:userId name:model.nickName portrait:model.avatar];
            dispatch_async(dispatch_get_main_queue(), ^{
                return completion(userInfo);
            });
        } failure:^(id  _Nonnull result) {
        }];
    }
}

@end



//@implementation HJNRCDataSource
//
//+(HJNRCDataSource *)shareInstance {
//    static HJNRCDataSource * _dataSource = nil;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        _dataSource = [[[self class] alloc] init];
//    });
//    return _dataSource;
//}
//
//#pragma mark -- RCIMGroupInfoDataSource 获取群组消息
//-(void)getGroupInfoWithGroupId:(NSString *)groupId completion:(void (^)(RCGroup *))completion {
//    if ([groupId length]==0) return;
//     // 从自己服务器获取群组用户信息 传给融云SDK
//    NSString * url = [Api stringByAppendingString:@""];
//    [HJHttpTool get:url params:@{} success:^(HJResponeds * _Nonnull result) {
//        // 加载成功给群组信息赋值
//        RCGroup * groupInfo = [[RCGroup alloc]init];
//        groupInfo.groupId = groupId;
//        groupInfo.groupName = @"";
//        groupInfo.portraitUri = @"";
//        return completion(groupInfo);
//        
//    } failure:^(id  _Nonnull result) {
//        NSLog(@"%@",result);
//    }];
//}


//@end
